package com.example.springaidemo.controller.case2;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 默认系统文本
 *
 * @author stone
 * @date 2025/6/30 15:11
 */
@RestController
@RequestMapping("/case2")
@Slf4j
public class Case2Controller {

    @Resource
    @Qualifier("defaultTextChatClient")
    private ChatClient defaultTextChatClient;

    /**
     * 默认系统文本
     */
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux(@RequestParam("input") String input) {
        // input=讲个笑话
        return this.defaultTextChatClient.prompt()
                .user(input)
                .stream()
                .content();
    }


    @Resource
    @Qualifier("paramTextChatClient")
    private ChatClient paramTextChatClient;

    /**
     * 动态系统文本
     */
    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(@RequestParam("input") String input, @RequestParam("role") String role) {
        // input=聊一聊圆明园的故事吧，500字以内
        // role=数学老师/邪恶女巫
        return this.paramTextChatClient.prompt()
                .system(promptSystemSpec -> promptSystemSpec.param("role", role))
                .user(input)
                .stream()
                .content();
    }

}
